import psutil
import socket
import platform
import time
from notice.DingTalk import DingTalkPushUtil


def get_size(bytes,suffix="B"):
    """将字节数转换为合适的单位"""
    factor = 1024
    for unit in ["", "K", "M", "G", "T", "P"]:
        if bytes < factor:
            return f"{bytes:.2f} {unit}{suffix}"
        bytes /= factor    

def cpu_info():
    """获取cpu信息"""
    return {"cpu_count": psutil.cpu_count(logical=True),"cpu_percent": psutil.cpu_percent()}
    
def cpu_loadavg():
    """系统负载信息"""
    load1, load5, load15 = psutil.getloadavg()
    return {"load1":load1,"load5":load5,"load15":load15}

def check_memory():
    """"检查内存使用情况"""
    memory = psutil.virtual_memory()
    return {
        "total memory_gb": get_size(memory.total),
        "used_memory_gb": get_size(memory.used),
        "used memory_percent": memory.percent
    }

def check_swap():
    """检查交换内存使用情况"""
    swap = psutil.swap_memory()
    return {
        "swap_total_gb": get_size(swap.total),
        "swap_used_gb":get_size(swap.used),
        "swap used percent": swap.percent
        }

def check_disk_usage():
    """检查磁盘使用率"""
    partitions = psutil.disk_partitions()
    return {part.mountpoint:  psutil.disk_usage(part.mountpoint)._asdict() for part in partitions}

def check_disk_io():
    """检查磁盘IO情况"""
    return psutil.disk_io_counters(perdisk=True)

def check_last_boot_time():
    """获取系统上次启动的时间"""
    return time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(psutil.boot_time()))


def main():
    print("="*20, "系统信息", "="*20)
    print(f"主机名: {socket.gethostname()}")
    print(f"操作系统: {platform.system()}")
    print(f"系统版本: {platform.version()}")


    print("="*20, "CPU信息", "="*20)
    print("CPU核数: {} 个".format(cpu_info().get("cpu_count")))
    print("CPU使用率: {}%".format(cpu_info().get("cpu_percent")))

    print("="*20, "系统负载信息", "="*20)
    print("1分钟负载: {}".format(cpu_loadavg().get("load1")))
    print("5分钟负载: {}".format(cpu_loadavg().get("load5")))
    print("15分钟负载: {}".format(cpu_loadavg().get("load15")))

    print("="*20, "内存信息", "="*20)
    print("总内存大小: {}".format(check_memory().get("total memory_gb")))
    print("使用内存大小: {}".format(check_memory().get("used_memory_gb")))
    print("内存使用率: {}%".format(check_memory().get("used memory_percent")))

    print("="*20, "交换内存信息", "="*20)
    print("交换内存总大小: {}".format(check_swap().get("swap_total_gb")))
    print("使用交换内存大小: {}".format(check_swap().get("swap_used_gb")))
    print("交换内存使用率: {}%".format(check_swap().get("swap used percent"))) 


    print("="*20, "文件系统信息", "="*20)
    for mount,usage in check_disk_usage().items():
        print("文件系统名称:{0},使用率:{1}%,总大小:{2},使用大小:{3},剩余大小{4}".format(mount,usage['percent'],get_size(usage["total"]),get_size(usage['used']),get_size(usage["free"])))
    
    print("="*20, "磁盘I0信息", "="*20)
    for disk, io in check_disk_io().items():
            print(f"磁盘: {disk}, 读IO: {get_size(io.read_bytes)}, 写IO: {get_size(io.write_bytes)}, 读操作: {io.read_count}, 写操作: {io.write_count}")
    
    print("="*20, "系统重启时间", "="*20)
    print(f"\n系统最后重启时间: {check_last_boot_time()}")
        
if __name__ == "__main__":
    main()
    #DingTalkPushUtil().send_text(main())
    